<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/09 14:41
 * Blog：www.myblogs.xyz
 */

namespace app\common\model;


class ReadMessages extends Common
{
    protected $name = 'read_messages';
    protected $pk = 'read_id';

    public function isRead($target, $target_id, $messages_id)
    {
        return $this->readStatus($target, $target_id, $messages_id, 1);
    }

    public function isDel($target, $target_id, $messages_id)
    {
        return $this->readStatus($target, $target_id, $messages_id, 2);
    }

    protected function readStatus($target, $target_id, $messages_id, $status)
    {
        $data = $this->getFind(['target'=>$target,'target_id'=>$target_id,'messages_id'=>$messages_id]);
        if (empty($data)) return false;
        if ($data['is_read'] == 1 && $status == 1){
            return true;
        }
        if ($data['is_del'] == 1 && $status == 2){
            return true;
        }
        return false;
    }

    public function readMessage($target, $target_id, $messages_id)
    {
        $list_where['target'] = $target;
        $list_where['target_id'] = $target_id;
        $list_where['messages_id'] = $messages_id;
        if ($this->getExists($list_where)){
            $this->where($list_where)->update(['is_read'=>1]);
        }else{
            $list_where['is_read'] = 1;
            $this->insert($list_where);
        }
        return true;
    }

}